<?php
class tx_branch_tceHooks {

		//Хук который создает новую группу пользователей для менеджеров филиала и
		//учетную запись для руководителя филиала
	function processDatamap_preProcessFieldArray(&$incomingFieldArray, $table, $id, &$tce)
	{
		if($table == 'user_farm_branches' && substr($id, 0, 3) == 'NEW')
		{
			$config = unserialize($GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf']['user_farm_branches']);
		
			$res = $GLOBALS['TYPO3_DB']->sql_query('SELECT MAX(uid) as curUid FROM `be_users`');
			$curUID = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res);
			
			$data = array(
				"be_users" => array(
					"NEW345ds45" => array(
						"pid" => "0",
						"username" => str_replace('###uid###', $curUID['curUid'], $config['branch_leader_template']),
						"password" => md5($config['default_leader_pass']),
						"disabled" => 0,
						"lang" => "ru",
						"usergroup" => $config['branch_leader_group'],
						"usergroup_cached_list" => $config['branch_leader_group']
					)
				),
				"be_groups" => array(
					"NEW3sdf3sa" => array(
						"pid" => 0,
						"title" => str_replace('###branch_name###', $incomingFieldArray["name"], $config['branch_manager_template']),
						"subgroup" => $config['branch_manager_parent_group']
					)
				)
			);
			
			$localTCE = t3lib_div::makeInstance('t3lib_TCEmain');

			$localTCE->stripslashes_values = 0;
			$localTCE->start($data, array());
			$localTCE->process_datamap();
			
			$incomingFieldArray['leader_id'] = $localTCE->substNEWwithIDs["NEW345ds45"];
			$incomingFieldArray['group_id'] = $localTCE->substNEWwithIDs["NEW3sdf3sa"];
		}
	}
	
		//Хук который создает папку для хранения csv файлов с 
		//информацией о ценах и остатках товаров в филиале
	function processDatamap_afterDatabaseOperations($status, $table, $id, &$fieldArray, &$tce)
	{
		global $BE_USER, $TYPO3_CONF_VARS;
	
		if($table == 'user_farm_branches' && $status == 'new')
		{
			$importDir = "branch_".$tce->substNEWwithIDs[$id].'/';
			t3lib_div::mkdir_deep(PATH_site.'fileadmin/', 'import/prices/'.$importDir);
		}
	}
		
		//Хук который при удалении филиала удалят связанных с ним пользователей и информацию
		//о ценах и остатках товара в филиале
	function processCmdmap_preProcess($command, $table, $id, $fields, &$tce)
	{
		if ($table == 'user_farm_branches' && $command == 'delete')
		{
			$branch = t3lib_BEfunc::getRecord('user_farm_branches', $id);
			
			if (is_array($branch))
			{
				$cmd = array(
					"be_users" => array(
						$branch['leader_id'] => array(
							"delete" => 1
						)
					),
					"be_groups" =>array(
						$branch['group_id'] => array(
							"delete" => 1
						)
					)
				);
				
				$priceAndRestRecords = t3lib_BEfunc::getRecordsByField(
					'user_farm_commerce_product_branch_mm',
					'branch_id',
					$id
				);
				
				if (is_array($priceAndRestRecords))
				{
					$cmd['user_farm_commerce_product_branch_mm'] = array();
					
					foreach($priceAndRestRecords as $priceAndRestRecord)
					{
						$cmd['user_farm_commerce_product_branch_mm'][$priceAndRestRecord['uid']] = array(
								"delete" => 1
						);
					}	
				}
				
				$localTCE = t3lib_div::makeInstance('t3lib_TCEmain');

				$localTCE->stripslashes_values = 0;
				$localTCE->start(array(), $cmd);
				$localTCE->process_cmdmap ();
			}
		}
	}
}
?>